package com.bbs.toutiao.controller.admin;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.bbs.toutiao.entity.ArticleClassifyEntity;
import com.bbs.toutiao.exception.BaseException;
import com.bbs.toutiao.exception.ExceptionEnum;
import com.bbs.toutiao.service.admin.impl.ArticleServiceImpl;
import com.bbs.toutiao.utils.ArticleUtil;
import com.bbs.toutiao.utils.Result;
import com.bbs.toutiao.vo.article.ArticleClassRequestVo;
import com.bbs.toutiao.vo.article.ArticlePageRequestVo;
import com.bbs.toutiao.vo.comm.PageVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 描述: 文章操作，查询分类，修改分类，查询文章
 * 作者： zhangBin
 * 时间： 2023/9/24 17:33
 * 版本： 1.0
 */

@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Resource
    private ArticleServiceImpl articleService;

    @Resource
    private ArticleUtil articleUtil;

    /**
     * 分页查询分类
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/classByPage")
    public Result classByPage(@RequestParam(name = "page") int page, @RequestParam(name = "size") int size) {
        if (page < 1 || size < 1) {
            return Result.error(401, "请求的数据格式错误");
        }
        PageVo<ArticleClassifyEntity> userList = articleService.findByClassPager(page, size);
        return Result.success("查询成功", userList);
    }

    /**
     * 新增分类
     *
     * @param requestVo
     * @return
     */
    @PostMapping("/insertClassify")
    public Result insertClassify(@RequestBody ArticleClassRequestVo requestVo) {
        try {
            Assert.notBlank(requestVo.getClassName());
            Assert.notBlank(requestVo.getCreatedAccount());
        } catch (IllegalArgumentException e) {
            throw new BaseException(ExceptionEnum.NOT_NULL);
        }
        boolean inserted = articleService.insertArticleClassify(requestVo);
        if (inserted) {
            return Result.success("新增成功");
        } else {
            return Result.error(401, "新增失败");
        }
    }

    /**
     * 更新分类
     *
     * @param requestVo
     * @return
     */
    @PostMapping("/updateClassify")
    public Result updateClassify(@RequestBody ArticleClassRequestVo requestVo) {

        try {
            Assert.notBlank(String.valueOf(requestVo.getId()));
            Assert.notBlank(requestVo.getCreatedAccount());
            Assert.notBlank(requestVo.getClassName());
        } catch (IllegalArgumentException e) {
            throw new BaseException(ExceptionEnum.NOT_NULL);
        }
        boolean inserted = articleService.updateArticleClassify(requestVo);
        if (inserted) {
            return Result.success("更新成功");
        } else {
            return Result.error(401, "更新失败");
        }
    }

    /**
     * 动态分页查询文章
     *
     * @param requestVo
     * @return
     */
    @PostMapping("/findByArticlePage")
    public Result findByArticlePage(@RequestBody ArticlePageRequestVo requestVo) {
        if (requestVo.getPage() < 1 || requestVo.getSize() < 1) {
            return Result.error(401, "请求的数据格式错误");
        }
        requestVo.setPage(requestVo.getPage(), requestVo.getSize());
        return Result.success("查询成功", articleService.findByArticlePager(requestVo));
    }

    /**
     * 根据id获取文章内容
     *
     * @param id
     * @return
     */
    @GetMapping("/findByIdArticle/{id}/{userId}")
    public Result findByIdArticle(@PathVariable int id, @PathVariable int userId, HttpServletRequest request) {
        if (id > 0) {
            String article = articleService.findByIdArticle(id);
            log.info("用户id:{}", userId);
            if (userId > 0) {
                articleUtil.currentProtection(request.getRemoteAddr(), id, userId);
            }
            return Result.success("查询成功", article);
        } else {
            return Result.error(401, "数据为空");
        }
    }


    /**
     * 根据id批量修改文章状态
     *
     * @param articleVoList
     * @return
     */
    @PostMapping("/updateArticleStatus")
    public Result updateArticleStatus(@RequestBody List<Map<String, Object>> articleVoList) {
        if (CollUtil.isEmpty(articleVoList)) {
            return Result.error(404, "修改的数据不能为空");
        } else {
            int i = articleService.updateArticle(articleVoList);
            return Result.success("修改了" + i + "条数据");
        }
    }
}
